package cn.jiedh.app.meiweidk.loan;


import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.umeng.analytics.MobclickAgent;

import org.xutils.ex.HttpException;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import cn.jiedh.app.meiweidk.MyWebActivity;
import cn.jiedh.app.meiweidk.R;
import cn.jiedh.app.meiweidk.Utils.UI.CommonButton;
import cn.jiedh.app.meiweidk.Utils.UI.EmptyRecyclerView;
import cn.jiedh.app.meiweidk.Utils.UI.GridItemDecoration;
import cn.jiedh.app.meiweidk.Utils.UI.StateLayout;
import cn.jiedh.app.meiweidk.Utils.UI.VerticalSpaceItemDecoration;
import cn.jiedh.app.meiweidk.Utils.tool.ActivityUtil;
import cn.jiedh.app.meiweidk.Utils.tool.DensityUtil;
import cn.jiedh.app.meiweidk.Utils.tool.L;
import cn.jiedh.app.meiweidk.Utils.tool.SPUtils;
import cn.jiedh.app.meiweidk.Utils.tool.T;
import cn.jiedh.app.meiweidk.Utils.tool.UrlUtils;
import cn.jiedh.app.meiweidk.app.MyApp;
import cn.jiedh.app.meiweidk.base.BaseFragment;
import cn.jiedh.app.meiweidk.baseadapter.BaseAdapter;
import cn.jiedh.app.meiweidk.baseadapter.BaseViewHolder;
import cn.jiedh.app.meiweidk.buried.BuriedBean;
import cn.jiedh.app.meiweidk.entity.LoanListBean;
import cn.jiedh.app.meiweidk.entity.TypeListBean;
import cn.jiedh.app.meiweidk.entity.productLinkTransferBean;
import cn.jiedh.app.meiweidk.network.ResponseFailure;
import cn.jiedh.app.meiweidk.network.ResponseSuccess;
import cn.jiedh.app.meiweidk.response.BaseResponse;


/**
 * @name：产品列表
 * @author：司马林
 * @phone：18423134135
 * @createTime：2017/10/23
 */
public class FragmentLoan extends BaseFragment implements PopupWindow.OnDismissListener {
    @BindView(R.id.bar_tv_title)
    TextView title;
    @BindView(R.id.bar_img_back)
    ImageView backImg;
    @BindView(R.id.title_layout)
    LinearLayout titleLayout;
    @BindView(R.id.chose_money_tv)
    TextView choseMoneyTv;
    @BindView(R.id.chose_type_tv)
    TextView choseTypeTv;
    @BindView(R.id.smart_sorting_tv)
    TextView sortTv;
    @BindView(R.id.condition_container)
    LinearLayout conditionContainer;
    @BindView(R.id.loan_rv)
    EmptyRecyclerView rv;
    @BindView(R.id.smartrefreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.state_layout)
    StateLayout stateLayout;
    @BindView(R.id.empty_nn_btn)
    TextView emptyTv;
    @BindView(R.id.empty_nn_pic)
    ImageView emptyImg;

    private int pageSize = 10;
    private int pageNo = 1;
    private int typeId = 0;

    private int moneyMax = 0;
    private int moneyMin = 0;
    private int sortId = 0;

    private int moneySelectIndex = 0;
    private int typeSelectIndex = 0;
    private int sortSelectIndex = 0;

    private PopupWindow moneyOptionPop;
    private PopupWindow typeOptionPop;
    private PopupWindow sortOptionPop;

    private BaseAdapter<LoanListBean.DataList> adapter;
    private BaseAdapter<TypeListBean.DataList> typeAdapter;
    private BaseAdapter<String> moneyAdapter;
    private BaseAdapter<String> sortAdapter;

    private List<LoanListBean.DataList> list = new ArrayList<>();
    private List<TypeListBean.DataList> typeList = new ArrayList<>();

    private View view;
    private List<BuriedBean> buriedList;

    @Override
    protected int getLayoutView() {
        return R.layout.fragment_loan;
    }

    @Override
    protected void initView(View view) {
        title.setText("贷款列表");
        this.view = view;
        emptyTv.setText("没有贷款产品哦～");
        emptyImg.setImageResource(R.mipmap.mydkcp);
        backImg.setVisibility(View.INVISIBLE);

        rv.setEmptyView(view.findViewById(R.id.empty_view));


        TypeListBean.DataList typeListBean = new TypeListBean.DataList();
        typeListBean.setName("所有贷款类型");
        typeListBean.setTypeid(0);
        typeList.add(typeListBean);




        // 向 titleLayout 注册方法，以获取控件尺寸
        ViewTreeObserver vto = titleLayout.getViewTreeObserver();
        vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                int height = (int) (DensityUtil.getScreenHeight(getActivity()) - conditionContainer.getY() - conditionContainer.getHeight() - titleLayout.getHeight());
                titleLayout.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                initMoneyOptionPop(height);
                initTypeOptionPop(height);
                initSortOptionPop(height);
            }
        });


        rv.setLayoutManager(new LinearLayoutManager(getActivity()));
        rv.addItemDecoration(new VerticalSpaceItemDecoration(1, false));
        adapter = new BaseAdapter<LoanListBean.DataList>(getActivity(), R.layout.loan_list_item, list) {
            @Override
            public void convert(BaseViewHolder holder, LoanListBean.DataList bean, int position) {
                holder.setText(R.id.loan_title, bean.getName());
                holder.setText(R.id.loan_advert, bean.getAdver());
                String rate = "";
                if (1 == bean.getRateType()) {
                    rate = bean.getRate() + "%";
                } else if (2 == bean.getRateType()) {
                    rate = bean.getRate() + "%";
                }
                holder.setText(R.id.loan_daily_rate, "参考"+bean.getRateTypeStr()+"利率"+rate);
//                holder.setText(R.id.rate_type_tv,"参考"+bean.getRateTypeStr()+"利率");
                double successRate = Double.valueOf(bean.getSuccessRate().trim());
                String successRateStr = (int) successRate + "%";
                holder.setText(R.id.loan_success_rate, "通过率"+successRateStr);
                String moneyMin = bean.getMoneyMin() + "";
                String moneyMax = bean.getMoneyMax() + "";
                if (moneyMin.length() >= 6) {
                    moneyMin = bean.getMoneyMin() / 10000 + "";
                } else {
                    moneyMin = bean.getMoneyMin() + "";
                }
                if (moneyMax.length() >= 6) {
                    moneyMax = bean.getMoneyMax() / 10000 + "万";
                } else {
                    moneyMax = bean.getMoneyMax() + "";
                }
                holder.setText(R.id.loan_quota, moneyMin + "-" + moneyMax);
                if (0 == bean.getHotWhether()) {
                    holder.getView(R.id.hot_img).setVisibility(View.GONE);
                } else if (1 == bean.getHotWhether()) {
                    holder.getView(R.id.hot_img).setVisibility(View.VISIBLE);
                }
                holder.setImageUrl(R.id.loan_icon, bean.getImgUrl());
                holder.setOnclickListener(R.id.item_layout, v -> {
                    MobclickAgent.onEvent(getActivity(), "Loan_item" + (position + 1));
                    buriedList = new ArrayList<BuriedBean>();
                    buriedList.add(new BuriedBean("click", "", "0", bean.getProductId() + "", bean.getName(), "", "false", "", "", "",
                            "", "", "", "", "", bean.getRate() + "", ""));
                    SPUtils.saveBuriedList(buriedList);
                    Bundle bundle = new Bundle();
                    bundle.putString("title", bean.getName());
                    bundle.putInt("productId", bean.getProductId());
                    ActivityUtil.next(getActivity(), LoanDetailActivity.class, bundle);
                });


                TextView goApply_tv=holder.getView(R.id.goApply_tv);
                goApply_tv.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        apply(bean.getProductId(),bean.getApplyUrl(),bean.getName());
                    }
                });
            }
        };
        rv.setAdapter(adapter);
        refreshLayout.autoRefresh();
        refreshAndLoad();
        stateLayout.setOnNoClick(view1 -> refreshLayout.autoRefresh());

    }






    /**
     * 统计并跳转
     *
     * @param productId
     * @param applyUrl
     * @param loanTitle
     */
    private void apply(int productId, String applyUrl, String loanTitle) {
        showLoadingDialog();
        Map<String, Object> map = new HashMap<>();
        map.put("productId", productId + "");
        request(UrlUtils.ADDBROWSE, map, false, new ResponseSuccess<String>() {
            @Override
            public void onRequestSucceed(BaseResponse response) {
                dismissLoadingDialog();
                if (0 == response.getCode()) {
//                    Bundle bundle = new Bundle();
//                    bundle.putString("url", applyUrl);
//                    bundle.putString("title", loanTitle);
//                    Intent intent = new Intent(getActivity(), MyWebActivity.class);
//                    intent.putExtras(bundle);
//                    startActivityForResult(intent, 1);
                    productLinkTransfer(productId,applyUrl,loanTitle);
                } else {
                    T.showShort(response.getMessage());
                }
            }
        }, new ResponseFailure() {
            @Override
            public void onRequestFailure(HttpException error, String msg) {
                T.show(msg);
                dismissLoadingDialog();
            }
        });
    }

    /**
     * 是否是联合登陆的产品
     */
    private void productLinkTransfer(int productId,String applyUrl,String loanTitle){
        showLoadingDialog();
        Map<String, Object> map = new HashMap<>();
        map.put("productId", productId + "");
        request(UrlUtils.PRODUCT_LINK_TRANSFER, map, false, new ResponseSuccess<productLinkTransferBean>() {
            @Override
            public void onRequestSucceed(BaseResponse<productLinkTransferBean> response) {
                dismissLoadingDialog();
                Intent intent = new Intent(getActivity(), MyWebActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString("title", loanTitle);
                if (0 == response.getCode()) {
                    bundle.putString("url", response.getData().getApplyUrl());
                } else {
                    bundle.putString("url", applyUrl);
                }
                intent.putExtras(bundle);
                startActivityForResult(intent, 1);
            }
        }, new ResponseFailure() {
            @Override
            public void onRequestFailure(HttpException error, String msg) {
                T.show(msg);
                dismissLoadingDialog();
            }
        });
    }

    @Override
    protected void initData() {
        getTypeList();
    }

    private void getTypeList() {
        Map<String, Object> map = new HashMap<>();
        request(UrlUtils.LOANTYPELIST, map, false, new ResponseSuccess<TypeListBean>() {
            @Override
            public void onRequestSucceed(BaseResponse<TypeListBean> response) {
                if (0 == response.getCode()) {
                    typeList.addAll(response.getData().getDataList());
                    typeAdapter.notifyDataSetChanged();
//                    getData(0, 0, typeList.get(0).getTypeid(), 0, 0, 0);
                }
            }
        }, (error, msg) -> {
            L.e("typeList", msg);
        });
    }

    private void refreshAndLoad() {
        refreshLayout.setOnRefreshListener(refreshLayout -> refreshLayout.getLayout().postDelayed(() -> {
            pageNo = 1;
            list.clear();
            stateLayout.netviewGone();
            adapter.notifyDataSetChanged();
            getData(moneyMin, moneyMax, typeId, 0, 0, sortId);
            refreshLayout.finishRefresh();
            refreshLayout.setLoadmoreFinished(false);
        }, 2000));
        refreshLayout.setOnLoadmoreListener(refreshLayout -> {
            refreshLayout.getLayout().postDelayed(() -> {
                getData(moneyMin, moneyMax, typeId, 0, 0, sortId);
                refreshLayout.finishLoadmore();
            }, 2000);

        });
    }

    /**
     * 获取贷款产品列表
     * @param moneyMin   最小金额
     * @param moneyMax   最大金额
     * @param typeId     类型ID
     * @param newWhether 是否新品
     * @param hotWhether 是否热门
     * @param sort       排序  0 智能排序 1成功率 2贷款利率 3贷款速度 4额度由高到低 5额度由低到高
     */
    private void getData(int moneyMin, int moneyMax, int typeId, int newWhether, int hotWhether, int sort) {
        showLoadingDialog();
        Map<String, Object> map = new HashMap<>();
        map.put("moneyMin", moneyMin);
        map.put("moneyMax", moneyMax);
        map.put("term", 0);
        map.put("typeId", typeId);
        map.put("newWhether", newWhether);
        map.put("hotWhether", hotWhether);
        map.put("sort", sort);
        map.put("pageNo", pageNo);
        map.put("pageSize", pageSize);



        L.i("pageNo-------"+pageNo);

        map.put("token", SPUtils.getToken(MyApp.mContext));//这个值是我加的

        request(UrlUtils.PRODUCTLIST, map, false, new ResponseSuccess<LoanListBean>() {
            @Override
            public void onRequestSucceed(BaseResponse<LoanListBean> response) {
                dismissLoadingDialog();
                if (0 == response.getCode()) {
                    if (response.getData().getDataList().size() <= 0) {
                        T.showShort("没有更多贷款产品啦");
                    }else{
                        pageNo++;
                        list.addAll(response.getData().getDataList());
                        adapter.notifyDataSetChanged();
                    }

                } else {
                    T.showShort(response.getMessage());
                }
            }
        }, (error, msg) -> {

        });
    }

    @OnClick({R.id.chose_money_layout, R.id.chose_type_layout, R.id.smart_sorting_layout})
    public void onClick(View view) {

        switch (view.getId()) {
            case R.id.chose_money_layout:
                choseMoneyTv.setTextColor(Color.parseColor("#fe0100"));

                choseMoneyTv.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.mipmap.downarrow, 0);
                choseTypeTv.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.mipmap.downarrowgray, 0);
                sortTv.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.mipmap.downarrowgray, 0);
                moneyOptionPop.showAsDropDown(conditionContainer);
                if (moneyAdapter != null) {
                    moneyAdapter.notifyDataSetChanged();
                }

                break;
            case R.id.chose_type_layout:
                choseTypeTv.setTextColor(Color.parseColor("#fe0100"));

                choseMoneyTv.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.mipmap.downarrowgray, 0);
                choseTypeTv.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.mipmap.downarrow, 0);
                sortTv.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.mipmap.downarrowgray, 0);
                typeOptionPop.showAsDropDown(conditionContainer);
                if (typeAdapter != null) {
                    typeAdapter.notifyDataSetChanged();
                }

                break;
            case R.id.smart_sorting_layout:
                sortTv.setTextColor(Color.parseColor("#fe0100"));

                choseMoneyTv.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.mipmap.downarrowgray, 0);
                choseTypeTv.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.mipmap.downarrowgray, 0);
                sortTv.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.mipmap.downarrow, 0);
                sortOptionPop.showAsDropDown(conditionContainer);
                if (sortAdapter != null) {
                    sortAdapter.notifyDataSetChanged();
                }
                break;
            default:
                break;
        }
    }

    private void initMoneyOptionPop(int height) {
        View moneyOptionView = getActivity().getLayoutInflater().inflate(R.layout.option_popupwindow, null);
        moneyOptionView.findViewById(R.id.blank_view).setOnClickListener(v -> moneyOptionPop.dismiss());

        if (moneyOptionPop != null && moneyOptionPop.isShowing()) {
            moneyOptionPop.dismiss();
        }
        moneyOptionPop = new PopupWindow(moneyOptionView, ViewGroup.LayoutParams.MATCH_PARENT, height);
        moneyOptionPop.setBackgroundDrawable(ContextCompat.getDrawable(getActivity(), R.color.no_color));
        moneyOptionPop.setFocusable(true);
        moneyOptionPop.setOutsideTouchable(true);
        moneyOptionPop.update();
        moneyOptionPop.setOnDismissListener(this);
        RecyclerView rv = moneyOptionView.findViewById(R.id.option_rv);
        rv.setLayoutManager(new GridLayoutManager(getActivity(), 3));
        rv.addItemDecoration(new GridItemDecoration(getResources().getDimensionPixelSize(R.dimen.x10)));
        List<String> list = new ArrayList<>();
        list.add("金额不限");
        list.add("3000以下");
        list.add("3000-5000");
        list.add("5000-10000");
        list.add("10000-20000");
        list.add("20000以上");

        moneyAdapter = new BaseAdapter<String>(getActivity(), R.layout.option_item, list) {
            @Override
            public void convert(BaseViewHolder holder, String s, int position) {
                CommonButton btn = holder.getView(R.id.option_common_btn);
                if (moneySelectIndex == position) {
                    btn.setTextColor(ContextCompat.getColor(getActivity(), R.color.white));
                    btn.setEnableColor(ContextCompat.getColor(getActivity(), R.color.red));
                } else {
                    btn.setTextColor(ContextCompat.getColor(getActivity(), R.color.textcolorgray));
                    btn.setEnableColor(ContextCompat.getColor(getActivity(), R.color.gary_bg));
                }
                btn.setText(list.get(position));
                btn.setOnClickListener(v -> {
                    buriedList = new ArrayList<BuriedBean>();
                    buriedList.add(new BuriedBean("query", "贷款列表-条件查询-金额", "0", "", "条件查询-金额", "贷款列表", "false", "", "", "", "", "", s, "", "", "", ""));
                    SPUtils.saveBuriedList(buriedList);
                    MobclickAgent.onEvent(getActivity(), "loan_time" + (position + 1));
                    moneySelectIndex = position;
                    choseMoneyTv.setText(s);
                    moneyOptionPop.dismiss();
                });
            }
        };
        rv.setAdapter(moneyAdapter);
    }


    private void initTypeOptionPop(int height) {
        View typeOptionView = getActivity().getLayoutInflater().inflate(R.layout.option_popupwindow, null);
        typeOptionView.findViewById(R.id.blank_view).setOnClickListener(v -> typeOptionPop.dismiss());

        if (typeOptionPop != null && typeOptionPop.isShowing()) {
            typeOptionPop.dismiss();
        }
        typeOptionPop = new PopupWindow(typeOptionView, ViewGroup.LayoutParams.MATCH_PARENT, height);
        typeOptionPop.setBackgroundDrawable(ContextCompat.getDrawable(getActivity(), R.color.no_color));
        typeOptionPop.setFocusable(true);
        typeOptionPop.setOutsideTouchable(true);
        typeOptionPop.update();
        typeOptionPop.setOnDismissListener(this);

        RecyclerView rv = typeOptionView.findViewById(R.id.option_rv);
        rv.setLayoutManager(new GridLayoutManager(getActivity(), 3));
        rv.addItemDecoration(new GridItemDecoration(getResources().getDimensionPixelSize(R.dimen.x10)));

        typeAdapter = new BaseAdapter<TypeListBean.DataList>(getActivity(), R.layout.option_item, typeList) {
            @Override
            public void convert(BaseViewHolder holder, TypeListBean.DataList s, int position) {
                CommonButton btn = holder.getView(R.id.option_common_btn);
                if (typeSelectIndex == position) {
                    btn.setTextColor(ContextCompat.getColor(getActivity(), R.color.white));
                    btn.setEnableColor(ContextCompat.getColor(getActivity(), R.color.red));
                } else {
                    btn.setTextColor(ContextCompat.getColor(getActivity(), R.color.textcolorgray));
                    btn.setEnableColor(ContextCompat.getColor(getActivity(), R.color.gary_bg));
                }
                btn.setText(typeList.get(position).getName());
                btn.setOnClickListener(v -> {
                    MobclickAgent.onEvent(getActivity(), "loan_type" + (position + 1));
                    buriedList = new ArrayList<BuriedBean>();
                    buriedList.add(new BuriedBean("query", "贷款列表-条件查询-贷款类型", "0", s.getTypeid() + "", "条件查询-贷款类型", "贷款列表", "false", "", "", "", "", "", "", "", "", "", s.getName()));
                    SPUtils.saveBuriedList(buriedList);
                    typeSelectIndex = position;
                    choseTypeTv.setText(typeList.get(position).getName());
                    typeId = typeList.get(position).getTypeid();
                    typeOptionPop.dismiss();
                });
            }
        };
        rv.setAdapter(typeAdapter);

    }

    private void initSortOptionPop(int height) {
        View moneyOptionView = getActivity().getLayoutInflater().inflate(R.layout.option_popupwindow, null);
        moneyOptionView.findViewById(R.id.blank_view).setOnClickListener(v -> sortOptionPop.dismiss());

        if (sortOptionPop != null && sortOptionPop.isShowing()) {
            sortOptionPop.dismiss();
        }
        sortOptionPop = new PopupWindow(moneyOptionView, ViewGroup.LayoutParams.MATCH_PARENT, height);
        sortOptionPop.setBackgroundDrawable(ContextCompat.getDrawable(getActivity(), R.color.no_color));
        sortOptionPop.setFocusable(true);
        sortOptionPop.setOutsideTouchable(true);
        sortOptionPop.update();
        sortOptionPop.setOnDismissListener(this);
        RecyclerView rv = moneyOptionView.findViewById(R.id.option_rv);
        rv.setLayoutManager(new GridLayoutManager(getActivity(), 3));
        rv.addItemDecoration(new GridItemDecoration(getResources().getDimensionPixelSize(R.dimen.x8)));
        List<String> list = new ArrayList<>();
        list.add("智能排序");
        list.add("成功率");
        list.add("贷款利率");
        list.add("贷款速度");
        list.add("额度由高到低");
        list.add("额度由低到高");

        sortAdapter = new BaseAdapter<String>(getActivity(), R.layout.option_item, list) {
            @Override
            public void convert(BaseViewHolder holder, String s, int position) {
                CommonButton btn = holder.getView(R.id.option_common_btn);
                if (sortSelectIndex == position) {
                    btn.setTextColor(ContextCompat.getColor(getActivity(), R.color.white));
                    btn.setEnableColor(ContextCompat.getColor(getActivity(), R.color.red));
                } else {
                    btn.setTextColor(ContextCompat.getColor(getActivity(), R.color.textcolorgray));
                    btn.setEnableColor(ContextCompat.getColor(getActivity(), R.color.gary_bg));
                }
                btn.setText(list.get(position));
                btn.setOnClickListener(v -> {
                    MobclickAgent.onEvent(getActivity(), "loan_sort" + (position + 1));
                    buriedList = new ArrayList<BuriedBean>();
                    buriedList.add(new BuriedBean("query", "贷款列表-条件查询-排序", "0", "", "条件查询-排序", "贷款列表", "false", "", "", "", "", "", "", "", s, "", ""));
                    SPUtils.saveBuriedList(buriedList);
                    sortSelectIndex = position;
                    sortTv.setText(s);
                    sortOptionPop.dismiss();
                });
            }
        };
        rv.setAdapter(sortAdapter);
    }

    @Override
    public void onDismiss() {
        Drawable arrow = ContextCompat.getDrawable(getActivity(), R.mipmap.downarrowgray);
        arrow.setBounds(0, 0, arrow.getIntrinsicWidth(), arrow.getIntrinsicHeight());
        choseMoneyTv.setTextColor(ContextCompat.getColor(getActivity(), R.color.textcolorgray));
        choseMoneyTv.setCompoundDrawables(null, null, arrow, null);
        choseTypeTv.setTextColor(ContextCompat.getColor(getActivity(), R.color.textcolorgray));
        choseTypeTv.setCompoundDrawables(null, null, arrow, null);
        sortTv.setTextColor(ContextCompat.getColor(getActivity(), R.color.textcolorgray));
        sortTv.setCompoundDrawables(null, null, arrow, null);

        switch (choseMoneyTv.getText().toString()) {
            case "金额不限":
                moneyMin = 0;
                moneyMax = 0;
                break;
            case "3000以下":
                moneyMin = 0;
                moneyMax = 3000;
                break;
            case "3000-5000":
                moneyMin = 3000;
                moneyMax = 5000;
                break;
            case "5000-10000":
                moneyMin = 5000;
                moneyMax = 10000;
                break;
            case "10000-20000":
                moneyMin = 10000;
                moneyMax = 20000;
                break;
            case "20000以上":
                moneyMin = 20000;
                moneyMax = 0;
                break;
            default:
                break;
        }
        switch (sortTv.getText().toString()) {
            case "智能排序":
                sortId = 0;
                break;
            case "成功率":
                sortId = 1;
                break;
            case "贷款利率":
                sortId = 2;
                break;
            case "贷款速度":
                sortId = 3;
                break;
            case "额度由高到低":
                sortId = 4;
                break;
            case "额度由低到高":
                sortId = 5;
                break;
            default:
                break;
        }
        rv.smoothScrollToPosition(0);
        refreshLayout.autoRefresh();
    }

    @Override
    public void onResume() {
        super.onResume();
        MobclickAgent.onResume(getActivity());
    }

    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPause(getActivity());
    }

}
